/*继承基model*/
var IssueModel = BaseModel.extend({
    objectClass: 'issue',
});
var IssueCollection = BaseCollection.extend({
    model: IssueModel
});
//定义view
var IssueItemView = Backbone.View.extend({
    tagName: 'a',
    className: "list-group-item",
    initialize: function() {
        this.listenTo(this.model, 'destroy', this.remove);
        this.listenTo(this.model, 'change', this.render);
    },
    events: {
        'click .sp-close': 'doRemove'
    },
    doRemove: function(e) {
        console.log(this.model)
        e.preventDefault()
        this.model.destroy({ wait: true })
    },
    _template: _.template($('#issue-item-view').html()),
    render: function() {
        var json = this.model.toJSON()
        // var html = '<h4 class="list-group-itme-heading">'+json.tite+'<small>'+json.objectId+'</h4>'
        // html += '<p class="list-group-itme-text">'+json.dsc+'</P>' ;
        this.$el.html(this._template(json))
        this.$el.attr('href', '#issue/' + json.objectId)
            //console.log(this.model.get('dsc'))
            // this.$el.html(this.model.get('tite'))            
        return this;
    }
})
var IssueCollectionView = BaseCollectionView.extend({
    subView: IssueItemView,
    initialize: function() {
        this._initialize()
    }
})
var PageIssueList = BasePage.extend({
    el: '#page-issue-list',
    initialize: function() {
        this.issueCollection = new IssueCollection();
        this.issueCollectionView = new IssueCollectionView({
            collection: this.issueCollection,
            el: '#issue-list'
        })
        this.issueCollection.fetch({ reset: true })
    },
});
var PageIssueCreate = BasePage.extend({
    el: '#page-issue-create',
    events: {
        'click #save-btn': 'doSave',
    },
    initialize: function(options) {
        this.router = options.router;
        this.collection = options.collection
    },
    doSave: function(e) {
        e.preventDefault();
        //console.log(1111)
        var _title = this.$el.find('#title').val();
        var _description = this.$el.find('#description').val();
        //创建新的model 
        var newIssue = new IssueModel({
            title: _title,
            description: _description
        })
        this.collection.create(newIssue, { wait: true })
        this.router.navigate('', { trigger: true })
    }
});
var PageIssueEdit = BasePage.extend({
    el: '#page-issue-edit',
    events: {
        'click #btn-save': 'doSave',
    },
    initialize:function(options){
        this.router = options.router;
    },
    show: function(issue) {
        if (issue) {
            console.log(issue)
            this.model = issue;
            this.render()
        }
        this.$el.show()
    },

    render: function() {
        var json = this.model.toJSON()
        this.$el.find('#title-edit').val(json.title)
        this.$el.find('#description-edit').val(json.description)
    },
     doSave: function(e) {
        e.preventDefault()
        var todo = AV.Object.createWithoutData('issue', '578cd053165abd00672ee91e');
        var _title = this.$el.find('#title-edit').val();
       
        var _description = this.$el.find('#description-edit').val();
      
        this.model.set({
            title:_title,
            description:_description

        });
        console.log(this.model)
       // $.ajax(url, settings, settings)
      // console.log(  this.model.save(null,{onlyChanged:true}))
        this.model.save(null,{onlyChanged:true})
         
        this.router.navigate('',{trigger:true})
    },
});
// /定义路由
var AppRouter = Backbone.Router.extend({
    initialize: function() {
        this.pageIssueList = new PageIssueList;
        this.pageIssueCreate = new PageIssueCreate({
            router: this,
            collection: this.pageIssueList.issueCollection
        });
        this.pageIssueEdit = new PageIssueEdit({
               router: this,
        });
    },
    hideAllPages: function() {
        this.pageIssueList.hide();
        this.pageIssueCreate.hide();
        this.pageIssueEdit.hide();
    },
    routes: {
        'issue/new': 'issueCreate',
        'issue/:id': 'issueEdit',
        '': 'issueList',
    },
    issueEdit: function(id) {
        this.hideAllPages();
        var issue = this.pageIssueList.issueCollection.find(function(model) {
            return model.id == id
        })
        this.pageIssueEdit.show(issue);
    },
    issueCreate: function() {
        this.hideAllPages();
        this.pageIssueCreate.show();
    },
    issueList: function() {
        this.hideAllPages();
        this.pageIssueList.show();
    }
});
var router = new AppRouter();
Backbone.history.start()
